﻿using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using System;
using System.Data;
using System.Drawing;

namespace eFatura.Utility
{
    public class RepositoryManager
    {
        public static void Init()
        {
            DataSet ds = AppSession.DataManager.ExecuteDataSet(AppSession.Schema + "GetLookups", AppSession.SirketId);
            Firma = GetGLESablon(ds.Tables[0], "sAciklama", "nFirmaID");
            Stok = GetGLESablon(ds.Tables[1], "sAciklama", "nStokID");
            Departman = GetGLESablon(ds.Tables[2], "Departman", "DepartmanId");
            Kullanici = GetGLESablon(ds.Tables[3], "AdSoyad", "KullaniciId");

            DataTable dt = new DataTable();
            dt.Columns.Add("ResponseStatus", typeof(int));
            dt.Columns.Add("OnayCevabi", typeof(string));
            dt.Rows.Add(-2, "Tekrar denenmeli");
            dt.Rows.Add(-1, "Başarısız");
            dt.Rows.Add(0, "Başarılı");
            FaturaOnayCevabi = GetGLESablon(dt, "OnayCevabi", "ResponseStatus");

            dt = new DataTable();
            dt.Columns.Add("FaturaOnay", typeof(int));
            dt.Columns.Add("Durum", typeof(string));
            dt.Rows.Add(1, "Onay");
            dt.Rows.Add(2, "Red");
            FaturaOnay = GetGLESablon(dt, "Durum", "FaturaOnay");

            dt = new DataTable();
            dt.Columns.Add("ProfileId", typeof(int));
            dt.Columns.Add("Senaryo", typeof(string));
            dt.Rows.Add(1, "Temel");
            dt.Rows.Add(2, "Ticari");
            Senaryo = GetGLESablon(dt, "Senaryo", "ProfileId");

            dt = new DataTable();
            dt.Columns.Add("FaturaTipi", typeof(int));
            dt.Columns.Add("Tip", typeof(string));
            dt.Rows.Add(1, "Satış");
            dt.Rows.Add(2, "İade");
            dt.Rows.Add(3, "Tevkifat");

            dt.Rows.Add(4, "İSTİSNAİ !");
            dt.Rows.Add(5, "ARAÇ TESCİL !");
            dt.Rows.Add(6, "ÖZEL MATRAF !");

            FaturaTipi = GetGLESablon(dt, "Tip", "FaturaTipi");

            dt = new DataTable();
            dt.Columns.Add("Cevap", typeof(string));
            dt.Columns.Add("Aciklama", typeof(string));
            dt.Rows.Add("?", "Onay Bekleniyor");
            dt.Rows.Add("E", "Onaylandı");
            dt.Rows.Add("H", "Reddedildi");
            Cevap = GetGLESablon(dt, "Aciklama", "Cevap");

            dt = new DataTable();
            dt.Columns.Add("SirketId", typeof(int));
            dt.Columns.Add("Sirket", typeof(string));
            dt.Rows.Add(1, "Karbel");
            dt.Rows.Add(2, "Karbel Dış");
            dt.Rows.Add(3, "Ersur");
            dt.Rows.Add(4, "Aker");
            dt.Rows.Add(5, "Sibel");
            Sirket = GetGLESablon(dt, "Sirket", "SirketId");
        }

        public static RepositoryItemGridLookUpEdit Firma { get; set; }

        public static RepositoryItemGridLookUpEdit Stok { get; set; }

        public static RepositoryItemGridLookUpEdit Departman { get; set; }

        public static RepositoryItemGridLookUpEdit Kullanici { get; set; }

        public static RepositoryItemGridLookUpEdit FaturaOnayCevabi { get; set; }

        public static RepositoryItemGridLookUpEdit FaturaOnay { get; set; }

        public static RepositoryItemGridLookUpEdit Senaryo { get; set; }

        public static RepositoryItemGridLookUpEdit FaturaTipi { get; set; }

        public static RepositoryItemGridLookUpEdit Cevap { get; set; }

        public static RepositoryItemGridLookUpEdit Sirket { get; set; }

        static void gleSablon_Popup(object sender, EventArgs e)
        {
            GridLookUpEdit gle = (GridLookUpEdit)sender;
            gle.Properties.View.BestFitColumns();
            gle.Popup -= gleSablon_Popup;
        }

        public static RepositoryItemGridLookUpEdit GetCustomRI(string caption, bool createRuntimeID, params string[] items)
        {
            string valueMember = caption;

            DataTable dataSource = new DataTable();

            dataSource.Columns.Add("Id", typeof(int));
            dataSource.Columns.Add(caption, typeof(string));

            for (int i = 0; i < items.Length; i++)
                dataSource.Rows.Add(new object[] { i + 1, items[i] });

            if (createRuntimeID)
                valueMember = "Id";
            else
                dataSource.Columns.RemoveAt(0);

            return RepositoryManager.GetGLESablon(dataSource, caption, valueMember);
        }

        public static RepositoryItemGridLookUpEdit GetGLESablon(DataTable SourceTable, string DisplayMember, string ValueMember)
        {
            RepositoryItemGridLookUpEdit gleSablon = new RepositoryItemGridLookUpEdit();
            gleSablon.PopupFilterMode = PopupFilterMode.Contains;
            //search columns via the View's OptionsFind.FindFilterColumns property. In the "StartsWith" or "Contains" mode, the search is performed against a field specified by the RepositoryItemSearchLookUpEdit.DisplayMember property.

            gleSablon.AllowNullInput = DefaultBoolean.True;
            gleSablon.View.OptionsView.ColumnAutoWidth = false;
            gleSablon.View.OptionsView.EnableAppearanceEvenRow = true;
            gleSablon.View.OptionsView.ShowAutoFilterRow = true;
            gleSablon.View.OptionsBehavior.Editable = false;
            gleSablon.View.BestFitMaxRowCount = 50;
            gleSablon.View.OptionsFind.HighlightFindResults = true;
            gleSablon.View.OptionsSelection.MultiSelectMode = GridMultiSelectMode.RowSelect;
            gleSablon.TextEditStyle = TextEditStyles.Standard; // DisableTextEditor;
            gleSablon.NullText = string.Empty;
            gleSablon.ImmediatePopup = true;

            gleSablon.PopupFormMinSize = new Size(500, 500);
            gleSablon.BestFitMode = BestFitMode.BestFit;

            gleSablon.Popup += gleSablon_Popup;

            gleSablon.DataSource = SourceTable;
            gleSablon.DisplayMember = DisplayMember;
            gleSablon.ValueMember = ValueMember;

            gleSablon.PopulateViewColumns();

            foreach (GridColumn gc in gleSablon.View.Columns)
            {
                gc.FilterMode = ColumnFilterMode.DisplayText;
            }

            return gleSablon;
        }
    }
}